//
//  GrantDetailViewController.m
//  FactorySale
//
//  Created by 李 on 2020/3/26.
//  Copyright © 2020 主事丫环. All rights reserved.
//
/**
 顶部的审核流程，待处理只有一个发起审核的状态
 tableview分两个section，第一个配件信息的section 通过返回row的行数来控制展开与折叠
 配件信息标题是 BuZhuJinZheDieCell一个单独的cell，配件信息详情为 BuZhuJinPeiJianInfoCell，申请补助金信息标题与内容为ShenQingBuZhuJinInfoCell
 */
#import "GrantDetailViewController.h"
#import "BuZhuJinZheDieCell.h"// 配件信息cell
#import "BuZhuJinPeiJianInfoCell.h"// 配件信息详情的cell
#import "ShenQingBuZhuJinInfoCell.h"// 申请补助金信息cell
#import "ShenHeViewController.h"// 审核控制器
#import "GrantDetailContentModel.h"
#import "GrantDetailStatesModel.h"
@interface GrantDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topNavBarConstraint;
@property (weak, nonatomic) IBOutlet UILabel *timelab;
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@property (nonatomic,strong) GrantDetailContentModel *detailModel;
@property (nonatomic,assign)BOOL peiJianInfoCellSelected;// 控制配件信息展开与闭合

@property (nonatomic,strong) GrantDetailStatesModel *statesModel;

@end
static NSString *const BuZhuJinZheDieCellID = @"BuZhuJinZheDieCell";
static NSString *const BuZhuJinPeiJianInfoCellID = @"BuZhuJinPeiJianInfoCell";
static NSString *const ShenQingBuZhuJinInfoCellID = @"ShenQingBuZhuJinInfoCell";

@implementation GrantDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.topNavBarConstraint.constant = kNaviHeight;
    self.view.backgroundColor = Main_BackGroundColor;
    [self setupTableView];
    [self requestGrantsDetail];
}

#pragma mark - 私有方法
-(void)setupTableView {
    [self.listTableView registerClass:[BuZhuJinZheDieCell class] forCellReuseIdentifier:BuZhuJinZheDieCellID];
    [self.listTableView registerClass:[BuZhuJinPeiJianInfoCell class] forCellReuseIdentifier:BuZhuJinPeiJianInfoCellID];
    [self.listTableView registerClass:[ShenQingBuZhuJinInfoCell class] forCellReuseIdentifier:ShenQingBuZhuJinInfoCellID];
    [self.view addSubview:self.listTableView];
    [self addFooterView];
}
- (IBAction)backBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 网络请求
-(void)requestGrantsDetail {
    NSDictionary *param = @{
        @"id":NOTNIL(self.idStr)
    };
    [NetWorkTool POST:grantsDetail param:param success:^(id dic) {
        
        self.detailModel = [GrantDetailContentModel mj_objectWithKeyValues:dic[@"data"]];
        self.statesModel = [GrantDetailStatesModel mj_objectWithKeyValues:dic[@"data"][@"states"]];
        
        [self refreshViewsData];
        [self.listTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
}
-(void)refreshViewsData {
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@  师傅发起申请",self.statesModel.createTime]];
    NSRange range1 = [[str string] rangeOfString:self.statesModel.createTime];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:range1];
    NSRange range2 = [[str string] rangeOfString:@"师傅发起申请"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#31CEA9"] range:range2];
    self.timelab.attributedText = str;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 0.5)];
    headerView.backgroundColor = Main_BackGroundColor;
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        if (self.peiJianInfoCellSelected ==YES) {
            return 2;
        } else {
            return 1;
        }
        return 1;
    }
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            BuZhuJinZheDieCell *cell = [tableView dequeueReusableCellWithIdentifier:BuZhuJinZheDieCellID];
            [cell.zhanKaiBtn addTarget:self action:@selector(zhanKaiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        } else {
            BuZhuJinPeiJianInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:BuZhuJinPeiJianInfoCellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }
    }
    
    if (indexPath.section==1) {
        ShenQingBuZhuJinInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:ShenQingBuZhuJinInfoCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.model = self.detailModel;
        return cell;
    }
    
    NSString *cellIdentifier = @"iden3";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section ==0) {
        if (indexPath.row==0) {
            return 37;
        }
        if (indexPath.row==1) {
            return 335;
        }
    } else {
        return 185;
    }
    return 0;
}
-(void)zhanKaiBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    UIView *view = [sender superview];
    BuZhuJinZheDieCell *cell = (BuZhuJinZheDieCell *)[view superview];
    if (sender.selected) {
        self.peiJianInfoCellSelected = YES;
        [cell.zhanKaiBtn setImage:[UIImage imageNamed:@"向上"] forState:UIControlStateNormal];
    } else {
        self.peiJianInfoCellSelected = NO;
        [cell.zhanKaiBtn setImage:[UIImage imageNamed:@"向下"] forState:UIControlStateNormal];
    }
    [self.listTableView reloadData];
}

-(void)addFooterView {
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 116)];
    
    UIButton *sureBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [sureBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    sureBut.frame  = CGRectMake(80, 50, KWIDTH - 160, 44);
    [sureBut setBackgroundImage:imgname(@"按钮背景") forState:UIControlStateNormal];
    [sureBut setTitle:@"去处理" forState:UIControlStateNormal];
    [sureBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBut.layer.masksToBounds = YES;
    sureBut.layer.cornerRadius = 22;
    [sureBut addTarget:self action:@selector(sureButAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [footerView addSubview: sureBut];
    
    self.listTableView.tableFooterView = footerView;
}

-(void)sureButAction:(UIButton *)btn {
    ShenHeViewController *vc = [[ShenHeViewController alloc]init];
    vc.idStr = self.detailModel.id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
